package com.example.ecas.controller;

import com.example.ecas.common.CommonResponse;
import com.example.ecas.persistence.DisRtagMapper;
import com.example.ecas.persistence.DiseaseMapper;
import com.example.ecas.persistence.TagMapper;
import com.example.ecas.persistence.WcRdisMapper;
import com.example.ecas.pojo.Case;
import com.example.ecas.pojo.DisRtag;
import com.example.ecas.pojo.WcRdis;
import com.example.ecas.service.CaseService;
import com.example.ecas.service.DiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RestController
public class DiseaseController {

    @Autowired
    DiseaseService diseaseService;

    @Autowired
    CaseService caseService;

    @Autowired
    WcRdisMapper wcRdisMapper;

    @Autowired
    DisRtagMapper disRtagMapper;

    @Autowired
    TagMapper tagMapper;

    // 获取被病类的统计信息
    // 返回: [ 包含该病的病历列表, 该病的病状词云图, 病状的饼图数据列表[[value],[name]] ]
    @GetMapping("/diseaseStatistics")
    @ResponseBody
    public CommonResponse<Object> getDisStatistics(@RequestParam("diseaseId") String id, HttpSession session) {
        int disId;
        try {
            disId = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return CommonResponse.creatForError("无效的病类ID类型");
        }
        List<Object> result = new ArrayList<>();
        result.add(caseService.getCasesByDiseaseId(disId));
        WcRdis wcRdis = wcRdisMapper.getWcRdis(disId);
        String wcB64 = wcRdis!=null ? wcRdis.getData() : diseaseService.getDisWordCloudB64(disId);
        if (wcRdis == null) {
            wcRdis = new WcRdis();
            wcRdis.setDisId(disId);
            wcRdis.setData(wcB64);
            wcRdisMapper.addWcRdis(wcRdis);
        }
        result.add(wcB64);

        List<DisRtag> disRtags = disRtagMapper.getDisRtagByDis(disId);
        List<Integer> valueList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        for (DisRtag disRtag : disRtags) {
            valueList.add((int) (disRtag.getRate()*100));
            nameList.add(tagMapper.getTag(disRtag.getTagId()).getContent());
        }
        List<Object> list = new ArrayList<>();
        list.add(valueList);
        list.add(nameList);
        result.add(list);
        return CommonResponse.creatForSuccess(result);
    }

    @GetMapping("/therapy")
    @ResponseBody
    public CommonResponse<Object> getDisStatistics(@RequestParam("diseaseId") String dId, @RequestParam("caseId") String cId, HttpSession session) {
        int disId, caseId;
        try {
            disId = Integer.parseInt(dId);
            caseId = Integer.parseInt(cId);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.creatForError("无效的病类ID类型或者病历ID类型");
        }
        List<Object> list = caseService.caseRecommendIncludeSimilarity(caseId);
        List<Case> cases1;
        List<Double> vals;
        try {
            cases1 = (List<Case>) list.get(0);
            vals = (List<Double>) list.get(1);
        } catch (Exception e) {
            e.printStackTrace();
            cases1 = new ArrayList<>();
            vals = new ArrayList<>();
        }
        List<String> percentVal = new ArrayList<>();
        double maxVal = vals.get(0) / 0.8;
        for (Double d : vals) {
            percentVal.add(String.valueOf((int) (d / maxVal * 100)) + "%" );
        }
        List<Case> cases2 = caseService.getCasesByDiseaseId(disId);
        List<Case> temp = new ArrayList<>();
        // 猛男式去重
        for (Case _case : cases2) {
            boolean existed = false;
            for (Case __case : cases1) {
                if (_case.getId() == __case.getId()) {
                    existed = true;
                    break;
                }
            }
            if (!existed) {
                temp.add(_case);
            }
        }
        for (Case _case : temp) {
            cases1.add(_case);
            percentVal.add("15%");
        }
        List<Object> result = new ArrayList<>();
        result.add(cases1);
        result.add(percentVal);
        return CommonResponse.creatForSuccess(result);
    }
}
